﻿using System;

using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace ControllerClient
{
    class TxtReader
    {
        String filepath = "";
        public TxtReader(String mfilepath) {
            filepath = mfilepath;
        }
         public string[] read()
        {
            int num = getLineNumber();
            string[] str = new string[num];
            FileStream fs = new FileStream(filepath, FileMode.Open);

            StreamReader m_streamReader = new StreamReader(fs, Encoding.Default);

            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            for (int i = 0; i < num; i++)
            {
                str[i] = m_streamReader.ReadLine();
            }
            fs.Close();
            m_streamReader.Close();
            return str;
        }
         public   string readAllLine()
         {
             int num = getLineNumber();
             StringBuilder sb = new StringBuilder();
             FileStream fs = new FileStream(filepath, FileMode.Open);

             StreamReader m_streamReader = new StreamReader(fs, Encoding.Default);

             m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

             for (int i = 0; i < num; i++)
             {
                 sb.Append(m_streamReader.ReadLine());
             }
             fs.Close();
             m_streamReader.Close();
             return sb.ToString();
         }
         public  string[] readUTF8()
         {
            string[] str=null;
            try
            {
                int num = getLineNumber();
                str = new string[num];
                FileStream fs = new FileStream(filepath, FileMode.Open);

                StreamReader m_streamReader = new StreamReader(fs, Encoding.UTF8);

                m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

                for (int i = 0; i < num; i++)
                {
                    str[i] = m_streamReader.ReadLine();
                    // Debug.WriteLine(" str[i]=" + str[i]);
                }
                fs.Close();
                m_streamReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("==" + e);
            }
            
             return str;
         }
         public int getLineNumber()
        {
            FileStream fs = new FileStream(filepath, FileMode.Open);

            StreamReader m_streamReader = new StreamReader(fs);

            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);

            string strLine = m_streamReader.ReadLine();

            int shu = 0;

            while (strLine != null)
            {
                strLine = m_streamReader.ReadLine();
                shu++;
            }
            fs.Close();
            m_streamReader.Close();
            return shu;
        }
    }
}
